//! ## 1. Two Sum (easy, array, hashmap)
//! Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.
//!
//! You may assume that each input would have exactly one solution, and you may not use the same element twice.
//!
//! You can return the answer in any order.
//! ### 知识点
//! - iter(), which iterates over &T.
//! - iter_mut(), which iterates over &mut T.
//! - into_iter(), which iterates over T.
//! - enumerate(), 内部元素是 (usize, T) 的迭代器

pub struct Solution;

use std::collections::HashMap;

impl Solution {
    pub fn solve(nums: Vec<i32>, target: i32) -> Vec<i32> {
        let mut hm: HashMap<i32, i32> = HashMap::new();
        for (i, &num) in nums.iter().enumerate() {
            if let Some(&j) = hm.get(&(target - num)) {
                return vec![j, i as i32];
            } else {
                hm.insert(num, i as i32);
            }
        }
        vec![]
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test() {
        let nums = vec![2, 7, 11, 15];
        let target = 9;
        assert_eq!(Solution::solve(nums, target), vec![0, 1]);
    }
}
